package com.aier.admin_new.service.impl;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Component;

import com.aier.admin_new.entity.SchoolMasterEmail;
import com.aier.admin_new.entity.SchoolMasterEmailDetail;
import com.aier.admin_new.entity.mapper.SchoolMasterEmailDetailRowMapper;
import com.aier.admin_new.entity.mapper.SchoolMasterEmailRowMapper;
import com.aier.admin_new.service.ISchoolMasterEmailService;
import com.aier.common.dao.ICommonDAO;

@Component("schoolMasterEmailService")
public class SchoolMasterEmailServiceImpl implements ISchoolMasterEmailService {
	@Resource(name = "commonDao")
    private ICommonDAO commonDao;
	
	@Override
	public long insertEmail(Long suid, String nickname,
			String headimg, String content, Integer sid) {
			SchoolMasterEmail  se = new SchoolMasterEmail();
			se.setCdate( new Timestamp( new Date().getTime() ) );
			se.setContent(content);
			se.setSuid(suid);
			se.setNickname(nickname);
			se.setSid(sid);
			KeyHolder kh = commonDao.insert( SchoolMasterEmailRowMapper.getPreparedStatementCreator(se),new GeneratedKeyHolder() );	
			 
		return kh.getKey().longValue();
	}

	
	@Override
	public int deleteEmail(int uid) {
		commonDao.update( "delete from  school_master_email_detail  where smeid = ? ", new Object[]{uid} );
		commonDao.update( "delete from  school_master_email  where smeid = ?  ", new Object[]{uid} );
		return 1;
	}

	@Override
	public List<SchoolMasterEmail> searchEmail(int sid) {
		List<SchoolMasterEmail> list = ( List<SchoolMasterEmail>) commonDao.list("SELECT * FROM"
				+ " (select l.* ,  u.headimg  from school_master_email  l  , user u where u.uid = l.suid and  l.sid=?  order by cdate desc ) "
				+ " BIAOMING  GROUP BY  suid ORDER BY smeid DESC LIMIT 10 ", new Object[]{sid}, new SchoolMasterEmailRowMapper() );
		return list;
	}
	
	@Override
	public long insertSchoolMasterEmailDetail(Long suid, Integer sid,
			String content, Integer smeid) {
		commonDao.update("insert into school_master_email_detail(suid,sid,content,cdate,smeid) values (?,?,?,?,?)",
              new Object[]{suid,sid,content,new Timestamp( new Date().getTime() ),smeid} );
		return 0;
	}


	@Override
	public List<SchoolMasterEmailDetail> searchEmailDetail(int smeid, int sid) {
		List<SchoolMasterEmailDetail> list =(List<SchoolMasterEmailDetail>) commonDao.list("select * from school_master_email_detail where smeid =? and sid =?   order by cdate desc ",new Object[]{smeid,sid},new SchoolMasterEmailDetailRowMapper() );
		return list;
	}


	@Override
	public List<SchoolMasterEmailDetail> searchEmailDetailByUid(int sid,long uid) {
		List<SchoolMasterEmailDetail> list =(List<SchoolMasterEmailDetail>) commonDao.list("select * from school_master_email_detail where  sid =?  and  suid =?    order by cdate desc ",new Object[]{sid,uid},new SchoolMasterEmailDetailRowMapper() );
		return list;
	}


	@Override
	public int searchEmailCount(int sid,long suid ) {
		return commonDao.countByQuali("select count(*)  from school_master_email where  sid  =  ? and suid =? ", new Object[]{sid,suid});
	}


	@Override
	public List<SchoolMasterEmail> searchEmail(int sid, long suid) {
		
		return  (List<SchoolMasterEmail>) commonDao.list("select *  from school_master_email where  sid  =  ? and suid =?  order by cdate desc ", new Object[]{sid,suid}, new SchoolMasterEmailRowMapper());
	}
	
}
